/*
 * Copyright (C) 2011 joaquin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package recursos;

import java.awt.*;

/**
 *
 * @author lzielinski
 */
public class ImageFitter {

    /**
     * TODO
     * @param img
     * @param height
     * @return 
     */
    public static Image fitHeight(Image img, int height) {
        int h = img.getHeight(null);
        double SF = h / (double) height;
        int w = (int) (img.getWidth(null) / SF);
        h = height;
        return img.getScaledInstance(h, w, Image.SCALE_DEFAULT);
    }

    /**
     * TODO
     * @param img
     * @param width
     * @return 
     */
    public static Image fitWidth(Image img, int width) {
        int w = img.getHeight(null);
        double SF = w / (double) width;
        int h = (int) (img.getHeight(null) / SF);
        w = width;
        return img.getScaledInstance(h, w, Image.SCALE_DEFAULT);
    }

    /**
     * TODO
     * @param img
     * @param side
     * @return 
     */
    public static Image fitSquare(Image img, int side) {
        if (img.getHeight(null) < img.getWidth(null)) {
            return fitWidth(img, side);
        } else {
            return fitHeight(img, side);
        }
    }
}
